
global control i.broad_sector i.ag_ano educ_diri_tert



use 	"$data\match_employer_employee_deidentify_panel_var.dta", clear

merge 	m:1 id wave using "$data\2_firm_ano_perfvars_individual_paper.dta", keepusing(strata_all_coll empcat broad_sector ag_ano educ_diri_tert)

duplicates drop id wave, force

eststo clear


foreach o in wage wrcon sosec {
	forval i=1/3 {
	forval a=0/1 {
	ren mean_match_`o' match
	ren mean_`o'_ind_higher ind_higher
	ren mean_`o'_emp_higher emp_higher
	
	eststo o_`o'_`i'_`a' : quietly estpost summarize match ind_higher emp_higher if  wave == `i' & assignment == `a' // `o'_panel == 3 &
		
	ren match  mean_match_`o'
	ren ind_higher  mean_`o'_ind_higher
	ren emp_higher mean_`o'_emp_higher
	}
	}
}

foreach o in wage wrcon sosec {
	forval s=1/3 {
	forval a=0/1 {
	ren mean_match_`o' mean_match
	ren mean_`o'_ind_higher mean_ind_higher
	ren mean_`o'_emp_higher mean_emp_higher
	
	eststo o_`o'_firm_size_`s'_`a' :  quietly estpost  summarize mean_match mean_ind_higher mean_emp_higher if  empcat == `s' & wave == 1 & assignment == `a'
		
	ren mean_match  mean_match_`o'
	ren mean_ind_higher  mean_`o'_ind_higher
	ren mean_emp_higher  mean_`o'_emp_higher
	}
	}
	}

*Baseline Table
esttab o_wage_1_* o_wrcon_1_* o_sosec_1_* using "$results\01_tables\Table_S11_consistency_check_baseline.tex", ///
    cells(mean(fmt(%5.2f))) label nodepvar noobs ///
    mtitle("Control" "Treatment" "Control" "Treatment" "Control" "Treatment") ///
    mgroups("Wages" "Written Contract" "Social Security", pattern(1 0 1 0 1 0) prefix(\multicolumn{2}{c}{) suffix(}) span) ///
    nonumbers ///
    varlabels(match "Consistent statements" ind_higher "Reported by employees only / higher" emp_higher "Reported by employers only / higher") ///
    prehead("\begin{tabular}{l*{6}{c}}\hline\hline") ///
    posthead("\hline \textbf{Panel A: All firms}\\") ///
    prefoot("\hline") ///
    postfoot("") ///
    replace

*Panel B.1: Firm size < 3 employees
esttab o_wage_firm_size_1_* o_wrcon_firm_size_1_* o_sosec_firm_size_1_* using "$results\01_tables\Table_S11_consistency_check_baseline.tex", ///
    cells(mean(fmt(%5.2f))) label nodepvar noobs ///
    nomtitles ///
    nonumbers ///
    varlabels(match "Consistent statements" ind_higher "Reported by employees only / higher" emp_higher "Reported by employers only / higher") ///
    prehead("") ///
    posthead("\textbf{Panel B.1: Firm size: < 3 employees}\\") ///
    prefoot("\hline") ///
    postfoot("") ///
    append

*Panel B.2: Firm size 4-6 employees  
esttab o_wage_firm_size_2_* o_wrcon_firm_size_2_* o_sosec_firm_size_2_* using "$results\01_tables\Table_S11_consistency_check_baseline.tex", ///
    cells(mean(fmt(%5.2f))) label nodepvar noobs ///
    nomtitles ///
    nonumbers ///
    varlabels(match "Consistent statements" ind_higher "Reported by employees only / higher" emp_higher "Reported by employers only / higher") ///
    prehead("") ///
    posthead("\textbf{Panel B.2: Firm size: 4-6 employees}\\") ///
    prefoot("\hline") ///
    postfoot("") ///
    append

*Panel B.3: Firm size 6+ employees
esttab o_wage_firm_size_3_* o_wrcon_firm_size_3_* o_sosec_firm_size_3_* using "$results\01_tables\Table_S11_consistency_check_baseline.tex", ///
    cells(mean(fmt(%5.2f))) label nodepvar noobs ///
    nomtitles ///
    nonumbers ///
    varlabels(match "Consistent statements" ind_higher "Reported by employees only / higher" emp_higher "Reported by employers only / higher") ///
    prehead("") ///
    posthead("\textbf{Panel B.3: Firm size: 6+ employees}\\") ///
    prefoot("\hline") ///
    postfoot("\hline\hline\end{tabular}") ///
    append
	
	
***Regression analysis

foreach var of varlist mean_wrcon_emp_higher mean_wage_emp_higher mean_sosec_emp_higher  {
		
			gen `var'_2021_temp = `var' if wave ==1
			bys id: egen `var'_2021 = max(`var'_2021_temp)
			drop `var'_2021_temp
			rename `var'_2021 `var'_raw_2021
					
				* Standardise LDV for different samples 
				egen `var'_2021=std(`var'_raw_2021)
				replace `var'_2021=0 if missing(`var'_raw_2021) 
				
				gen mis_`var'=1 if missing(`var'_raw_2021) 
				replace mis_`var'=0 if !missing(`var'_raw_2021)
			}

duplicates drop id wave, force	

*estimates clear
foreach o in mean_wrcon_emp_higher mean_wage_emp_higher mean_sosec_emp_higher {
areg `o' assignment $control i.wave mis_`o' `o'_2021  i.ag_ano if wave ==2, vce(cl id) a(strata_all_coll)
estimates store `o'_est_1

		qui summ `o' if assignment==0
		local mu : di %5.2f r(mean)
		estadd local mu `mu'

		
}

*estimates clear
foreach o in mean_wrcon_emp_higher mean_wage_emp_higher mean_sosec_emp_higher {
areg `o' assignment $control i.wave mis_`o' `o'_2021 i.ag_ano if wave ==3, vce(cl id) a(strata_all_coll)
estimates store `o'_est_2

		qui summ `o' if assignment==0
		local mu : di %5.2f r(mean)
		estadd local mu `mu'

}

esttab   mean_wage_emp_higher_est_1 mean_wage_emp_higher_est_2 mean_wrcon_emp_higher_est_1 mean_wrcon_emp_higher_est_2  mean_sosec_emp_higher_est_1 mean_sosec_emp_higher_est_2 using "$results\01_tables\Table_S15_robustness_missreporting.tex", scalars("mu Mean") keep(assignment) mtitle("6 M." "18 M."  "6 M." "18 M."  "6 M." "18 M."  ) mgroups("Sh. Min. Wage" "Sh. Written Contract" "Sh. Social Security", pattern(1 0  1 0 1 0 ) prefix(\multicolumn{@span}{c}{) suffix(})) se starlevels( * 0.10 ** 0.05 *** 0.01) replace  


